<?php namespace Admin\Controller;
use Hdphp\Controller\Controller;
/**
 * 货品控制器
 */
class GoodsListController extends CommonController {
	private $model;
	public function __init() {
		parent::__init();
		$this->model=new \Common\Model\GoodsList;
	}
	public function index()
	{
		$gid=Q('get.gid',0,'intval');
		$tid=Q('get.tid',0,'intval');
		$data=$this->model->where("goods_gid={$gid}")->get();
		//规格选择
		$spec=Db::table('type_att')->where("type_tid={$tid} and tatype=1")->field('taid,taname')->get();
		foreach ($spec as $k => $v)
		{
			$spec[$k]['opt']=Db::table('goods_att')->field('gaid', 'gavalue')->where("goods_gid={$gid} and type_att_taid={$v['taid']}")->get();
		}
		//已添加货品
		foreach ($data as $k => $v)
		{
			$data[$k]['spec']=Db::table('goods_att')->where("gaid in ({$v['gltid']})")->field('gavalue')->get();
		}
		View::with('data',$data);
 		View::with('spec',$spec);
		View::with('gid',$gid);
		View::make();
	}
	//添加
	public function add()
	{
		if(IS_POST){
			if(!$this->model->store()){
				$this->model->getError();
			}
			View::success('添加成功');
		}
	}
	//删除
	public function del()
	{
		$glid=Q('get.glid',0,'intval');
		$this->model->delete($glid);
		View::success('删除成功');
	}
	public function edit()
	{
		$glid=Q('get.glid',0,'intval');
		if(IS_POST){
			if(!$this->model->edit($glid)){
					$this->model->getError();
			}
			View::success('编辑成功');
		}
		$olddata=$this->model->where("glid={$glid}")->find();
		$olddata['spec']=Db::table('goods_att')->where("gaid in ({$olddata['gltid']})")->get();
		foreach ($olddata['spec'] as $k => $v) {
			$spec[$k]=Db::table('type_att')->where("taid={$v['type_att_taid']} and tatype=1")->first();
		}
		foreach ($spec as $k => $v)
		{
			$spec[$k]['opt']=Db::table('goods_att')->field('gaid', 'gavalue')->where("goods_gid={$olddata['goods_gid']} and type_att_taid={$v['taid']}")->get();
		}
		View::with('olddata',$olddata);
		View::with('spec',$spec);
		View::make();
	}
}